package artid.com.arttogo.adapter;

import java.util.ArrayList;
import java.util.List;
import android.R.integer;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import artid.com.arttogo.R;
import artid.com.arttogo.activity.CollecterActivity;
import artid.com.arttogo.bean.ArtistWorks;
import artid.com.arttogo.bean.GoodsList;
import artid.com.arttogo.bean.LikeGoods;
import artid.com.arttogo.db.Cache;
import artid.com.arttogo.network.AppUtil;
import artid.com.arttogo.network.BitmapCache;
import artid.com.arttogo.network.VolleyBasic;
import artid.com.arttogo.network.VolleyCallback;
import artid.com.arttogo.network.VolleyData;
import artid.com.arttogo.network.VolleyMap;
import artid.com.arttogo.task.CallbackSky;
import artid.com.arttogo.utils.MyBitmapImageViewTarget;
import artid.com.arttogo.utils.NetUtils;
import artid.com.arttogo.view.DynamicHeightImageView;

/**
 * 艺术商城列表适配器
 */
public class MallListAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater mInflater;
    ViewHolder holder;
    CallbackSky cs;
    public List<GoodsList> listdata;

    public MallListAdapter(Context mContext) {
        this.mContext = mContext;
        mInflater = LayoutInflater.from(mContext);
        listdata = new ArrayList<GoodsList>();
    }

    public void setdata(List<GoodsList> listdata) {
        this.listdata = listdata;
        // iconUrl = iconurl;
        notifyDataSetChanged();
    }

    public void addlist(List<GoodsList> listdata) {
        this.listdata.addAll(listdata);
        // iconUrl = iconurl;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return listdata.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_mall, null);
            vh.img = (DynamicHeightImageView) convertView.findViewById(R.id.item_mall_img);
            vh.name = (TextView) convertView.findViewById(R.id.item_mall_name);
            vh.price = (TextView) convertView.findViewById(R.id.item_mall_price);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        final GoodsList item = listdata.get(position);
        vh.name.setText(item.getGoods_name());
        if (item.getGoods_storage().equals("0")) {
            vh.price.setText("已售出");
        } else {
            vh.price.setText("¥ " + item.getGoods_price());
            if (item.getGoods_price().length()>8) {
                vh.price.setText("¥ " + "99999999+");
            }
        }

        vh.img.setHeight(item.getHeight());
        Glide.with(mContext).load(item.getGoods_image()
                + "_"+ (Integer.parseInt(NetUtils.getDisplayMetrics((Activity)mContext).get(NetUtils.DM_WIDTH)) /1) + "x10.png"
        ).asBitmap().placeholder(R.drawable.ic_menu_rotate).error(R.drawable.ic_menu_rotate).into(new MyBitmapImageViewTarget(vh.img));
        /*
        老版本处理逻辑未删除留着以后更改使用
        if (item.getIs_auth().equals("1")) {
            vh.recImg.setVisibility(View.VISIBLE);
            vh.certifImg.setVisibility(View.VISIBLE);
        } else {
            vh.recImg.setVisibility(View.INVISIBLE);
            vh.certifImg.setVisibility(View.INVISIBLE);
        }
        vh.size.setText(item.getGoods_size());
        vh.likenum.setText(item.getGoods_commend_num());
        if (item.getIs_like().equals("0")) {
            vh.unlikeImg.setVisibility(View.INVISIBLE);
            vh.likeImg.setVisibility(View.VISIBLE);
        } else {
            vh.unlikeImg.setVisibility(View.VISIBLE);
            vh.likeImg.setVisibility(View.INVISIBLE);
        }
        vh.time.setText(item.getGoods_addtime());
        vh.artistName.setText(item.getRealname());
        ViewGroup.LayoutParams para;
        para = vh.mainImg.getLayoutParams();

        para.height = item.getHeight();
        para.width = Integer.valueOf(AppUtil.deviceWidth);
        vh.mainImg.setLayoutParams(para);
        Glide.with(mContext).load(item.getGoods_image() + AppUtil.picWidthUrl)
                .asBitmap().placeholder(R.drawable.ic_menu_rotate).fitCenter()
                .error(R.drawable.ic_menu_rotate)
                .into(new MyBitmapImageViewTarget(vh.mainImg));

        // if (item.getArtistimg().contains("http")) {
        Glide.with(mContext)
                .load(item.getArtistimg() + "_" + "100" + "x10.png").asBitmap()
                .centerCrop().placeholder(R.drawable.ic_menu_rotate)
                .error(R.drawable.ic_menu_rotate)
                .into(new MyBitmapImageViewTarget(vh.artistImg));

        vh.iconview.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(mContext, CollecterActivity.class);
                intent.putExtra("id", item.getArtist_id());
                mContext.startActivity(intent);
            }
        });

        convertView.findViewById(R.id.item_mall_likeview).setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub

                        if (item.getIs_like().equals("0")) {
                            reqisLike(item.getGoods_id(), true, position);
                        } else {
                            reqisLike(item.getGoods_id(), false, position);
                        }

                    }
                });

        */

        return convertView;
    }

    class ViewHolder {
        DynamicHeightImageView img;
        TextView name,price;

    }

    void reqAtristListData(final String id) {
        VolleyBasic.getInstance((Activity) mContext).post(Cache.artistWorks,
                VolleyMap.getInstance().artistgoodsList(id, "1", "20", "400"),
                ArtistWorks.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        List<GoodsList> item = (List<GoodsList>) vd.getList();
                        if (item.isEmpty()) {
                            return;
                        }
                        setdata(item);
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }

    String itemstate = "";

    void reqisLike(String id, final Boolean isLike, final int position) {
        if (AppUtil.LoginVer(mContext)) {
            return;
        }
        if (isLike) {
            itemstate = "1";
        } else {
            itemstate = "0";
        }
        VolleyBasic.getInstance((Activity) mContext).post(Cache.likeGoods,
                VolleyMap.getInstance().likeGoods(id, itemstate),
                LikeGoods.class, new VolleyCallback() {

                    @Override
                    public void onSuccess(VolleyData vd) {
                        // TODO Auto-generated method stub
                        LikeGoods itemArtShownum = (LikeGoods) vd.getData();
                        if (vd.getCode() == 1) {
                            GoodsList item = listdata.get(position);
                            item.setIs_like(itemstate);
                            String num = "";
                            if (isLike) {
                                num = String.valueOf(Integer.valueOf(item
                                        .getGoods_commend_num()) + 1);
                            } else {
                                num = String.valueOf(Integer.valueOf(item
                                        .getGoods_commend_num()) - 1);
                            }
                            item.setGoods_commend_num(num);
                            listdata.set(position, item);
                            notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(VolleyError volleyError) {
                        // TODO Auto-generated method stub

                    }
                });
    }

}
